using System;

namespace PaulRz.Util.Patterns
{
    /// <summary>
    /// Implements the MS pattern of disposable and finalizable classes
    /// </summary>
    public abstract class DisposableFinalizable : IDisposable
    {
        
        ~DisposableFinalizable()
        {
            Dispose(false);
        }
        
        ///<summary>
        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        ///</summary>
        ///<filterpriority>2</filterpriority>
        public void Dispose()
        {
            Dispose(true);
        }
        
        private void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                GC.SuppressFinalize(this);
                CleanManaged();
            }
            CleanUnmanaged();
        }

        /// <summary>
        /// This method cleans the manages resources
        /// </summary>
        protected abstract void CleanManaged();

        /// <summary>
        /// This method cleans the unmanages resources
        /// </summary>
        protected abstract void CleanUnmanaged();
        
    }
}
